package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @Reference
    private MenuService menuService;
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findMenuids")
    public Result findMenuids(Integer id) {
        try {
            Integer[] menuids = roleService.findMenuids(id);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS, menuids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/findMenuAll")
    public Result findMenuAll() {
        try {
            List<Map> menumap = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menumap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/findpermissionAll")
    public Result findpermissionAll() {
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/findpermissionAllByroleid")
    public Result findpermissionAllByroleid(Integer id) {
        try {
            Integer[] permissionids = roleService.findPermissionids(id);
            return new Result(true, MessageConstant.QUERY_PERMISSIONID_SUCCESS, permissionids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIONID_FAIL);
        }
    }

    @RequestMapping("/findByid")
    public Result findByid(Integer id) {
        try {
            Role role = roleService.findByid(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] menuids, Integer[] permissionids) {
        try {
            String keyword = role.getKeyword();
            //判断是否包含 ROLE_
            if (!keyword.contains("ROLE_")){
                String s = "ROLE_"+keyword;
                role.setKeyword(s);
            }
            roleService.add(role, menuids, permissionids);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/deleteByid")
    public Result deleteByid(Integer id){
        try {
            roleService.deleteByid(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Role role, Integer[] menuids, Integer[] permissionids){
        try {
            String keyword = role.getKeyword();
            //判断是否包含 ROLE_
            if (!keyword.contains("ROLE_")){
                String s = "ROLE_"+keyword;
                role.setKeyword(s);
            }
            roleService.update(role, menuids, permissionids);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
}
